import elib.Ksiazka;
import elib.Osoba;
import elib.Klient;
import elib.Kierownik;
import elib.Bibliotekarz;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet("/SearchServlet")
public class SearchServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public SearchServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		List<Ksiazka> listaKsiazek = null;
		String[] params = new String[7];
		params[0] = request.getParameter("tytul");
		params[1] = request.getParameter("autor");
		params[2] = request.getParameter("wydano");
		params[3] = request.getParameter("stron");
		params[4] = request.getParameter("isbn");
		params[5] = request.getParameter("dostep");
		params[6] = request.getParameter("fragment");
		if (session != null) {
			Osoba osoba = (Osoba) session.getAttribute("osoba");
			if (osoba != null) {
				listaKsiazek = osoba.przegladajKsiegozbior(params);
				session.setAttribute("ksiazki", listaKsiazek);
				if(osoba instanceof Kierownik)
					response.sendRedirect("admin.jsp?page=books");
				else if(osoba instanceof Bibliotekarz)
					response.sendRedirect("librarian.jsp?page=books");
				else if(osoba instanceof Klient)
					response.sendRedirect("user.jsp?page=books");
			} else {
				listaKsiazek = new Osoba().przegladajKsiegozbior(params);
				session.setAttribute("ksiazki", listaKsiazek);
				response.sendRedirect("index.jsp?page=books");
			}
		}
		out.close();
	}

}
